package cn.knowei.miaosha.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: knowei
 * @Description:
 * @Date: Create in 15:12 2023-04-07
 */
@Configuration
public class RabbitMQConfiguration {
    public static final String DELAY_EXCHANGE_NAME = "delay.exchange";
    public static final String DELAY_QUEUE_A_NAME = "delay.queue.a";
    public static final String DELAY_QUEUE_B_NAME = "delay.queue.b";
    public static final String DELAY_QUEUE_A_ROUTING_KEY = "delay.queue.a.routingkey";
    public static final String DELAY_QUEUE_B_ROUTING_KEY = "delay.queue.b.routingkey";

    //死信交换机
    public static final String DEAD_LETTER_EXCHANGE_NAME = "dead.letter.exchange";
    //私信队列
    public static final String DEAD_LETTER_QUEUE_A_NAME = "dead.letter.queue.a";
    public static final String DEAD_LETTER_QUEUE_B_NAME = "dead.letter.queue.b";
    //
    public static final String DEAD_LETTER_QUEUE_A_ROUTING_KEY = "dead.letter.delay_10.routingkey";
    public static final String DEAD_LETTER_QUEUE_B_ROUTING_KEY = "dead.letter.delay_60.routingkey";

    // 声明延迟交换机
    @Bean("delayExchange")
    public DirectExchange delaytExchange() {
        return new DirectExchange(DELAY_EXCHANGE_NAME);
    }

    @Bean("deadLetterExchange")
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(DEAD_LETTER_EXCHANGE_NAME);
    }


    //声明延迟队列A
    @Bean("delayQueueA")
    public Queue delayQueueA() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_A_ROUTING_KEY);
        args.put("x-message-ttl", 10000);
        return QueueBuilder.durable(DELAY_QUEUE_A_NAME)
                .withArguments(args)
                .build();
    }

    //声明延迟队列B
    @Bean("delayQueueB")
    public Queue delayQueueB() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key", DEAD_LETTER_QUEUE_B_ROUTING_KEY);
        args.put("x-message-ttl", 60000);
        return QueueBuilder.durable(DELAY_QUEUE_B_NAME)
                .withArguments(args)
                .build();
    }

    // 设置延迟队列A绑定
    @Bean
    public Binding delayBindingA(@Qualifier("delayQueueA") Queue queue,
                                 @Qualifier("delayExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(DELAY_QUEUE_A_ROUTING_KEY);
    }

    // 设置延迟队列A绑定
    @Bean
    public Binding delayBindingB(@Qualifier("delayQueueB") Queue queue,
                                 @Qualifier("delayExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(DELAY_QUEUE_B_ROUTING_KEY);
    }

    //声明死信队列A
    @Bean("deadLetterQueueA")
    public Queue deadLetterQueueA() {
        return new Queue(DEAD_LETTER_QUEUE_A_NAME);
    }

    //声明死信队列B
    @Bean("deadLetterQueueB")
    public Queue deadLetterQueueB() {
        return new Queue(DEAD_LETTER_QUEUE_B_NAME);
    }

    // 设置死信队列A绑定
    @Bean
    public Binding deadLetterBindingA(@Qualifier("deadLetterQueueA") Queue queue,
                                 @Qualifier("deadLetterExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(DEAD_LETTER_QUEUE_A_ROUTING_KEY);
    }

    // 设置死信队列B绑定
    @Bean
    public Binding deadLetterBindingB(@Qualifier("deadLetterQueueB") Queue queue,
                                      @Qualifier("deadLetterExchange") DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).with(DEAD_LETTER_QUEUE_B_ROUTING_KEY);
    }

}
